/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.gatech.architourists;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class PhotoGalleryActivity extends Activity implements
		AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_photo_gallery);

		mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
		mSwitcher.setFactory(this);
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		Gallery g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(this));
		g.setOnItemSelectedListener(this);
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		mSwitcher.setImageResource(mImageIds[position]);
	}

	public void onNothingSelected(AdapterView<?> parent) {
	}

	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		return i;
	}

	private ImageSwitcher mSwitcher;

	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
//			ImageView i = new ImageView(mContext);
//
//			i.setImageResource(mThumbIds[position]);
//			i.setAdjustViewBounds(true);
//			i.setLayoutParams(new Gallery.LayoutParams(
//					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//			i.setBackgroundResource(R.drawable.picture_frame);
//			return i;
//			
			if(convertView == null)
		      { // create new view
		          convertView = new ImageView(mContext);
		          convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		      }
		     // ImageView iv = ;
		      BitmapFactory.Options options=new BitmapFactory.Options();
		      options.inPurgeable=true; //bitmap can be purged to disk
		      Bitmap bm=BitmapFactory.decodeResource(mContext.getResources(), mThumbIds[position], options);
		      ((ImageView)convertView).setImageBitmap(bm);
		      return (ImageView)convertView;
			
//			 ImageView i = new ImageView(mContext);
//
//		        i.setImageResource(mThumbIds[position]);
//		        i.setAdjustViewBounds(true);
//		        i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//		        i.setBackgroundResource(R.drawable.picture_frame);
//		        return i;
		}

		private Context mContext;

	}

	private Integer[] mThumbIds = { R.drawable.i_barcelona_cathedral1,
			R.drawable.i_barcelona_cathedral2,
			R.drawable.i_barcelona_cathedral3,
			R.drawable.i_barcelona_cathedral4,
			R.drawable.i_barcelona_cathedral5,
			R.drawable.i_barcelona_pavillion1,
			R.drawable.i_barcelona_pavillion2,
			R.drawable.i_barcelona_pavillion3,
			R.drawable.i_barcelona_pavillion4,
			R.drawable.i_barcelona_pavillion5, R.drawable.i_casa_batllo1,
			R.drawable.i_casa_batllo2, R.drawable.i_casa_batllo3,
			R.drawable.i_casa_batllo4, R.drawable.i_casa_batllo5,
			R.drawable.i_casa_mila1, R.drawable.i_casa_mila2,
			R.drawable.i_casa_mila3, R.drawable.i_casa_mila4,
			R.drawable.i_casa_mila5, R.drawable.i_parc_guell1,
			R.drawable.i_parc_guell2, R.drawable.i_parc_guell3,
			R.drawable.i_parc_guell4, R.drawable.i_parc_guell5,
			R.drawable.i_picasso_museum1, R.drawable.i_picasso_museum2,
			R.drawable.i_picasso_museum3, R.drawable.i_placa_espanya1,
			R.drawable.i_placa_espanya2, R.drawable.i_placa_espanya3,
			R.drawable.i_placa_espanya4, R.drawable.i_placa_espanya5,
			R.drawable.i_sagrada_familia1, R.drawable.i_sagrada_familia2,
			R.drawable.i_sagrada_familia3, R.drawable.i_sagrada_familia4,
			R.drawable.i_sagrada_familia5 };

	private Integer[] mImageIds = { R.drawable.barcelona_cathedral1,
			R.drawable.barcelona_cathedral2,
			R.drawable.barcelona_cathedral3,
			R.drawable.barcelona_cathedral4,
			R.drawable.barcelona_cathedral5,
			R.drawable.barcelona_pavillion1,
			R.drawable.barcelona_pavillion2,
			R.drawable.barcelona_pavillion3,
			R.drawable.barcelona_pavillion4,
			R.drawable.barcelona_pavillion5, R.drawable.casa_batllo1,
			R.drawable.casa_batllo2, R.drawable.casa_batllo3,
			R.drawable.casa_batllo4, R.drawable.casa_batllo5,
			R.drawable.casa_mila1, R.drawable.casa_mila2,
			R.drawable.casa_mila3, R.drawable.casa_mila4,
			R.drawable.casa_mila5, R.drawable.parc_guell1,
			R.drawable.parc_guell2, R.drawable.parc_guell3,
			R.drawable.parc_guell4, R.drawable.parc_guell5,
			R.drawable.picasso_museum1, R.drawable.picasso_museum2,
			R.drawable.picasso_museum3, R.drawable.placa_espanya1,
			R.drawable.placa_espanya2, R.drawable.placa_espanya3,
			R.drawable.placa_espanya4, R.drawable.placa_espanya5,
			R.drawable.sagrada_familia1, R.drawable.sagrada_familia2,
			R.drawable.sagrada_familia3, R.drawable.sagrada_familia4,
			R.drawable.sagrada_familia5 };

}
